/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Desarrollo.Soft.Hibernate.ViewPrincipal.views;

import Desarrollo.Soft.Hibernate.Control.ctrlGeneric;
import Desarrollo.Soft.Hibernate.Data.Entity.Catproductos;
import Desarrollo.Soft.Hibernate.Data.Entity.Tblproductosproveedores;
import Desarrollo.Soft.Hibernate.ViewPrincipal.frmPantallaPrincipal;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author r0ck3r
 */
public class intFrmProductosProveedores extends javax.swing.JInternalFrame {

    /**
     * Creates new form intFrmProductosProveedores
     */
    private intFrmProveedores frmProveedores;
    private List<Catproductos> listaProducto;
    private frmPantallaPrincipal pantallaPrincipal;
    
    public intFrmProductosProveedores() {
        initComponents();
        cargarCombo();
    }
      public intFrmProductosProveedores(intFrmProveedores _proveedor, frmPantallaPrincipal _frmPrincipal) {
        initComponents();
        cargarCombo();
        this.frmProveedores = _proveedor;
        this.pantallaPrincipal = _frmPrincipal;
    }

    public void cargarCombo()
    {
        try {
            listaProducto = ctrlGeneric.<Catproductos>selectItems(Catproductos.class);
        
            
            cmbProductos.removeAllItems();
            cmbProductos.addItem("");
            for (int i = 0; i < listaProducto.size(); i++) {
                cmbProductos.addItem(listaProducto.get(i).getValor());
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }
    
    public void seleccionarProductoAgragado(Catproductos _producto)
    {
        try {
            cmbProductos.setSelectedItem(_producto.getValor());
        } catch (Exception e) {
             System.out.print(e.getMessage());
        }
    }
    
    private boolean validaciondatos()
    {
        try {
            if(cmbProductos.getSelectedItem().toString().equals(""))
            {
                return false;
            }
            if(txtNumProductos.getText().equals(""))
            {
                return false;
            }
            if(txtPrecioMayoreo.getText().equals(""))
            {
                return false;
            }
             if(txtprecioUnitario.getText().equals(""))
            {
                return false;
            }
            return true;
        } catch (Exception e) {
            throw  e;
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cmbProductos = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtprecioUnitario = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtPrecioMayoreo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtNumProductos = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jLabel6 = new javax.swing.JLabel();
        btnguardar = new javax.swing.JButton();
        btncancelar = new javax.swing.JButton();
        btnagregarProductos = new javax.swing.JButton();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Productos"));

        jLabel1.setText("Producto : ");

        jLabel2.setText("Precio Unitario:");

        jLabel3.setText("Precio Mayoreo :");

        jLabel4.setText("Numero Productos :");

        jLabel5.setText("para Mayoreo");

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jLabel6.setText("                                                               ");
        jToolBar1.add(jLabel6);

        btnguardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Desarrollo/Soft/Hibernate/ViewPrincipal/Imagenes/Save.gif"))); // NOI18N
        btnguardar.setText("Guardar");
        btnguardar.setFocusable(false);
        btnguardar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnguardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnguardarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnguardar);

        btncancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Desarrollo/Soft/Hibernate/ViewPrincipal/Imagenes/Delete.png"))); // NOI18N
        btncancelar.setText("Cancelar");
        btncancelar.setFocusable(false);
        btncancelar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btncancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btncancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelarActionPerformed(evt);
            }
        });
        jToolBar1.add(btncancelar);

        btnagregarProductos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Desarrollo/Soft/Hibernate/ViewPrincipal/Imagenes/Add.gif"))); // NOI18N
        btnagregarProductos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnagregarProductosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbProductos, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnagregarProductos, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtNumProductos))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(txtPrecioMayoreo))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(27, 27, 27)
                                .addComponent(txtprecioUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(cmbProductos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnagregarProductos, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtprecioUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtPrecioMayoreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtNumProductos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnagregarProductosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnagregarProductosActionPerformed

        try {
            pantallaPrincipal.agregarProducto(this);
       } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }//GEN-LAST:event_btnagregarProductosActionPerformed

    private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnguardarActionPerformed
        try {
            if(this.validaciondatos())
            {
                Tblproductosproveedores productProveedores = new Tblproductosproveedores();
                productProveedores.setCantidadproductos(Integer.parseInt(txtNumProductos.getText()));
                productProveedores.setPrecioMayoreo(Double.parseDouble(txtPrecioMayoreo.getText()));
                productProveedores.setPrecioUnitario(Double.parseDouble(txtprecioUnitario.getText()));
                productProveedores.setCatproductos(listaProducto.get(cmbProductos.getSelectedIndex()));
                this.frmProveedores.agregarProducto(productProveedores);
                this.frmProveedores.cargarProductos();
                this.dispose();
                
            }
            else{
                JOptionPane.showMessageDialog(null, "Todos los datos son obligatorios.");
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }//GEN-LAST:event_btnguardarActionPerformed

    private void btncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelarActionPerformed
       if((txtNumProductos.getText().equals(""))||(txtPrecioMayoreo.getText().equals(""))
               ||(txtprecioUnitario.getText().equals(""))||(cmbProductos.getSelectedItem().toString().equals("")))
       {
           this.dispose();
       }
       else
       {
           if(JOptionPane.showConfirmDialog(null, "Seguro que desea Cancelar la operacion ? ") == 0)
           {
               this.dispose();
           }
       }
    }//GEN-LAST:event_btncancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnagregarProductos;
    private javax.swing.JButton btncancelar;
    private javax.swing.JButton btnguardar;
    private javax.swing.JComboBox cmbProductos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField txtNumProductos;
    private javax.swing.JTextField txtPrecioMayoreo;
    private javax.swing.JTextField txtprecioUnitario;
    // End of variables declaration//GEN-END:variables
}
